package com.shuyun.api.provider.controller;

import cn.hutool.core.util.URLUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.Map;

/**
 * @description:
 * @author: shuai.chen@shuyun.com
 * @date: 2023/1/6 11:24 AM
 */
@Slf4j
@RestController
@RequestMapping("api/test")
public class UrlEncoderTest {

    public static final String SCRIPT_JS_DATA = "<script type=\"text/javascript\"> window.location.assign(\"%s\") </script>";
    public static final String topParameter = "abc+ddd";

    @GetMapping("jumpPage")
    public String jumpPage(){
        Map<String, String> requestParam = new HashMap<>();
        requestParam.put("topParameter", topParameter);
        String redirectUrl = String.format("%s/api-provider/v1/api/test/printParam?%s",
                "http://localhost:8090",
                URLUtil.buildQuery(requestParam, Charset.defaultCharset()));
        return String.format(SCRIPT_JS_DATA, redirectUrl);
    }

    @GetMapping("printParam")
    public String printParam(@RequestParam Map<String, String> param){
        log.info("param:{}", param);
        String result = param.get("topParameter").equals(topParameter) ? "答：相同" : "答：不同";
        return "跳转后topParameter是否相等? \n" + result;
    }

}
